'''
Created on 2009/6/15

@author: Hung-Hsi Chen
'''

import urllib
import urllib2
import string
from lxml import etree

def fetchTWSEStockID():
    url = 'http://www.twse.com.tw/ch/trading/exchange/BWIBBU/BWIBBU_d.php'
    stockid = []
    try:
        response = urllib2.urlopen(url)
        content = response.read()
#        print content
        html = etree.HTML(content)
        table = html.xpath('//div[@id="tbl-containerx"]/table//tr/td[@class="basic2"][1]')
        for id  in table:
            stockid.append(str(id.text))
#            print id.text
        
        print "len of stock id = %s"%len(stockid)
        return tuple(stockid)
        #todo parse stock id from content
    except urllib2.HTTPError:
        print "%s not found!!"%url

if __name__ == '__main__':
    print fetchTWSEStockID()